<?php
namespace app\admin\controller;
use app\model\Category as CategoryModel;
use app\model\Module2 as Module2Model;
use think\Db;


class Module2 extends Base
{
    public function __construct(){
        $model = new Module2Model();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '文章';

        if ( in_array($page_info['action'], ['add','edit']) ) {
            $fields = [
                'title'=>['name'=>'标题','type'=>'text'],
                'subtitle'=>['name'=>'副标题','type'=>'text'],
                'description'=>['name'=>'描述','type'=>'textarea'],
                // 'image'=>['name'=>'图片','type'=>'file'],
                'url'=>['name'=>'链接','type'=>'text'],
                'content'=>['name'=>'内容','type'=>'textarea']
                // 'content'=>['name'=>'内容','type'=>'ueditor']
            ];
            $valids = createPageValid($fields);
            $this->assign([
                'fields'=>$fields,
                'valids'=>$valids
            ]);
        }

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    // public function index(){
    //     $model = $this->model;
    //     $map_params = [
    //         ['key'=>'title','type'=>'like']
    //     ];
    //     $map = getSearchParam($map_params);
      
    //     $search_params = [
    //         [
    //             ['key'=>'title','name'=>'标题','type'=>'text'],
    //         ]
    //     ];
    //     $this->assign([
    //         'search_params'=>$search_params
    //     ]);

    //     return parent::index($map);
    // }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'title','type'=>'like'],
            ['key'=>'category_id','type'=>'=']
        ];
        $map = getSearchParam($map_params);
      
        $search_params = [
            [
                ['key'=>'title','name'=>'标题','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',10)
        ];
        $list = $model->where($map)->order('ctime','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['title'],
                'nomust'=>['subtitle','description','url','content']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['attribute'])) {
                sort($save_data['attribute']);
                $save_data['attribute'] = implode(',', $save_data['attribute']);
            }
        }
        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        $id = 1;
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'must'=>['title'],
                'nomust'=>['subtitle','description','url','content']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }
        
        $model = $this->model;
        if (request()->isPost()) {
            $t_fields = $model->getTableFields();
            if (!isset($save_data['utime']) && in_array('utime', $t_fields)) {
                $save_data['utime'] = time();
            }
            if ( $model->where('id',$id)->update($save_data) !== false ) {
                $this->success("编辑成功",url('edit'));
            }else{
                $this->error("编辑失败");
            }
        }
        $data = $model->where('id',$id)->find();
        return view(emptyDef($this->page_info['view'],'common/save'), [
            'data'=>$data
        ]);
    }

    
}
